package hhh.com.dianming;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class add extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name,et_user;
    private Button bt_submit;
    private String name,user;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        initView();
    }

    private void initView(){
        et_name = (EditText) findViewById(R.id.et_name);
        et_user = (EditText) findViewById(R.id.et_user);
        bt_submit = (Button) findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(this);
        sp = getSharedPreferences("user",MODE_PRIVATE);
        editor = sp.edit();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_submit:
                name = et_name.getText().toString().trim();
                user = et_user.getText().toString().trim();

                if(name.equals("")||user.equals("")){
                    Toast.makeText(this,"学生信息不完整！",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                        MySqlite mySQLite = new MySqlite(this);
                        db= mySQLite.getWritableDatabase();
                        ContentValues values=new ContentValues();
                        values.put("name",name);
                        values.put("user",user);
                        db.insert("mings", null, values);
                        db.close();
                        Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                        finish();
                }
                break;
        }
    }
}
